<template>
  <div class="mod-sys__rawmaterialsconfig">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-input v-model="state.dataForm.materialName" placeholder="原材料名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="state.datetimerange" type="datetimerange" value-format="YYYY-MM-DD HH:mm:ss"
          :range-separator="$t('datePicker.range')" :start-placeholder="$t('datePicker.start')"
          :end-placeholder="$t('datePicker.end')"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="state.datetimerange" type="datetimerange" value-format="YYYY-MM-DD HH:mm:ss"
          :range-separator="$t('datePicker.range')" :start-placeholder="$t('datePicker.start')"
          :end-placeholder="$t('datePicker.end')"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="state.exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:rawmaterialsconfig:save')" type="primary"
          @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:rawmaterialsconfig:delete')" type="danger"
          @click="state.deleteHandle()">{{ $t("deleteBatch") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border
      @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <!-- <el-table-column prop="configId" label="原材料配置的唯一标识符" header-align="center" align="center"></el-table-column> -->
      <el-table-column prop="dishId" label="菜品id" header-align="center" align="center"></el-table-column>
      <el-table-column prop="materialName" label="材料名称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="materialSpecification" label="原材料规格" header-align="center"
        align="center"></el-table-column>
      <el-table-column prop="unitOfMeasure" label="计量单位" header-align="center" align="center"></el-table-column>
      <el-table-column prop="quantity" label="原材料数量" header-align="center" align="center"></el-table-column>
      <el-table-column prop="supplierId" label="供应商ID" header-align="center" align="center"></el-table-column>
      <el-table-column prop="qualityStandard" label="质量标准" header-align="center" align="center"></el-table-column>
      <el-table-column prop="costPerUnit" label="单价" header-align="center" align="center"></el-table-column>
      <el-table-column prop="totalCost" label="总成本" header-align="center" align="center"></el-table-column>
      <el-table-column prop="alternativeMaterials" label="原材料替代品" header-align="center"
        align="center"></el-table-column>
      <el-table-column prop="expirationDate" label="保质期" header-align="center" align="center"></el-table-column>
      <el-table-column prop="notes" label="备注" header-align="center" align="center"></el-table-column>
      <el-table-column prop="createdAt" label="创建时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="updatedAt" label="更新时间" header-align="center" align="center"></el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('sys:rawmaterialsconfig:update')" type="primary" link
            @click="addOrUpdateHandle(scope.row.configId)">{{ $t("update") }}</el-button>
          <el-button v-if="state.hasPermission('sys:rawmaterialsconfig:delete')" type="primary" link
            @click="state.deleteHandle(scope.row.configId)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
      :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle"
      @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update :key="addKey" ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { nextTick, reactive, ref, toRefs, watch } from "vue";
import AddOrUpdate from "./rawmaterialsconfig-add-or-update.vue";

const view = reactive({
  getDataListURL: "/sys/rawmaterialsconfig/page",
  getDataListIsPage: true,
  exportURL: "/sys/rawmaterialsconfig/export",
  deleteURL: "/sys/rawmaterialsconfig",
  deleteIsBatch: true,
  datetimerange: null,
  dataForm: {
    materialName: "",
    startDateTime: null as number | null,
    endDateTime: null as number | null,
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

watch(
  () => state.datetimerange,
  (val: number[] | null) => {
    if (val === null) {
      state.dataForm.startDateTime = null;
      state.dataForm.endDateTime = null;
    } else {
      state.dataForm.startDateTime = val[0];
      state.dataForm.endDateTime = val[1];
    }
  }
);
watch(
  () => state.datetimerange,
  (val: number[] | null) => {
    if (val === null) {
      state.dataForm.startDateTime = null;
      state.dataForm.endDateTime = null;
    } else {
      state.dataForm.startDateTime = val[0];
      state.dataForm.endDateTime = val[1];
    }
  }
);

const addKey = ref(0);
const addOrUpdateRef = ref();
const addOrUpdateHandle = (configId?: number) => {
  addKey.value++;
  nextTick(() => {
    addOrUpdateRef.value.init(configId);
  });
};
</script>